﻿Public Class KakePakke
    Public Property id As ULong
    Public Property utsalgsPris As Single
    Public Property innkjopsPris As Single
    Public Property kakeId As ULong
    Public Property vasresalgId As ULong

    Public Sub New()

    End Sub

    Public Sub New(_id As ULong)
        id = _id
    End Sub

    Public Function beregnInnkjopsPris() As Single

        Dim oppskriften As KakeOppskrift = KakeOppskrift.FinnById(kakeId)
        Dim ingrediensOgAntall As Dictionary(Of UInteger, UInteger) = oppskriften.FinnIngrediensOgAntall

        Dim enIngrediens As New Ingrediens
        Dim innPris As Single = 0

        Dim par As KeyValuePair(Of UInteger, UInteger)
        For Each par In ingrediensOgAntall
            enIngrediens = Ingrediens.FinnById(par.Key)
            innPris += (enIngrediens.innkjopspris * par.Value)
        Next

        Return innPris

    End Function

    Public Function beregnUtsalgsPris() As Single

        Dim oppskriften As KakeOppskrift = KakeOppskrift.FinnById(kakeId)
        Dim ingrediensOgAntall As Dictionary(Of UInteger, UInteger) = oppskriften.FinnIngrediensOgAntall

        Dim enIngrediens As New Ingrediens
        Dim utPris As Single = 0

        Dim par As KeyValuePair(Of UInteger, UInteger)
        For Each par In ingrediensOgAntall
            enIngrediens = Ingrediens.FinnById(par.Key)
            utPris += (enIngrediens.utsalgspris * par.Value)
        Next

        Return utPris

    End Function


    Public Sub Lagre(ByVal vsID)

        Dim søkestreng As String = "INSERT INTO kakepakke (kakepakkeUtsalgsPris, kakepakkeInnkjopsPris, kakeId, varesalgsId)"
        søkestreng &= " VALUES ('" & utsalgsPris & "','" & innkjopsPris & "','" & kakeId & "','" & vsID & "')"

        Dim dt As New DataTable
        dt = Database.Query(søkestreng)

    End Sub


    Public Shared Function FinnAlle() As List(Of KakePakke)

        Dim søkestreng As String = "SELECT * FROM kakepakke"
        Dim kakepakkeTabell As New DataTable
        kakepakkeTabell = Database.Query(søkestreng)
        Dim kakepakkeListe As New List(Of KakePakke)

        Dim row As DataRow
        For Each row In kakepakkeTabell.Rows
            Dim enKakePakke As New KakePakke
            enKakePakke.id = row("kakePakkeNr")
            enKakePakke.utsalgsPris = row("kakePakkeUtsalgsPris")
            enKakePakke.innkjopsPris = row("kakePakkeInnkjopsPris")
            enKakePakke.kakeId = row("kakeId")
            enKakePakke.vasresalgId = row("varesalgsId")

            kakepakkeListe.Add(enKakePakke)

        Next row
        'returnerer en liste med alle ingrediens objektene fra databasen
        Return kakepakkeListe

    End Function

End Class
